home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dumbdbm.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  7KB  |  220 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """A dumb and slow but simple dbm clone.
  5.  
  6. For database spam, spam.dir contains the index (a text file),
  7. spam.bak *may* contain a backup of the index (also a text file),
  8. while spam.dat contains the data (a binary file).
  9.  
  10. XXX TO DO:
  11.  
  12. - seems to contain a bug when updating...
  13.  
  14. - reclaim free space (currently, space once occupied by deleted or expanded
  15. items is never reused)
  16.  
  17. - support concurrent access (currently, if two processes take turns making
  18. updates, they can mess up the index)
  19.  
  20. - support efficient access to large databases (currently, the whole index
  21. is read when the database is opened, and some updates rewrite the whole index)
  22.  
  23. - support opening for read-only (flag = 'm')
  24.  
  25. """
  26. import os as _os
  27. import __builtin__
  28. import UserDict
  29. _open = __builtin__.open
  30. _BLOCKSIZE = 512
  31. error = IOError
  32.  
  33. class _Database(UserDict.DictMixin):
  34.     _os = _os
  35.     _open = _open
  36.     
  37.     def __init__(self, filebasename, mode):
  38.         self._mode = mode
  39.         self._dirfile = filebasename + _os.extsep + 'dir'
  40.         self._datfile = filebasename + _os.extsep + 'dat'
  41.         self._bakfile = filebasename + _os.extsep + 'bak'
  42.         self._index = None
  43.         
  44.         try:
  45.             f = _open(self._datfile, 'r')
  46.         except IOError:
  47.             f = _open(self._datfile, 'w')
  48.             self._chmod(self._datfile)
  49.  
  50.         f.close()
  51.         self._update()
  52.  
  53.     
  54.     def _update(self):
  55.         self._index = { }
  56.         
  57.         try:
  58.             f = _open(self._dirfile)
  59.         except IOError:
  60.             pass
  61.  
  62.         for line in f:
  63.             line = line.rstrip()
  64.             (key, pos_and_siz_pair) = eval(line)
  65.             self._index[key] = pos_and_siz_pair
  66.         
  67.         f.close()
  68.  
  69.     
  70.     def _commit(self):
  71.         if self._index is None:
  72.             return None
  73.         
  74.         try:
  75.             self._os.unlink(self._bakfile)
  76.         except self._os.error:
  77.             self._index is None
  78.             self._index is None
  79.         except:
  80.             self._index is None
  81.  
  82.         
  83.         try:
  84.             self._os.rename(self._dirfile, self._bakfile)
  85.         except self._os.error:
  86.             self._index is None
  87.             self._index is None
  88.         except:
  89.             self._index is None
  90.  
  91.         f = self._open(self._dirfile, 'w')
  92.         self._chmod(self._dirfile)
  93.         for key, pos_and_siz_pair in self._index.iteritems():
  94.             f.write('%r, %r\n' % (key, pos_and_siz_pair))
  95.         
  96.         f.close()
  97.  
  98.     sync = _commit
  99.     
  100.     def __getitem__(self, key):
  101.         (pos, siz) = self._index[key]
  102.         f = _open(self._datfile, 'rb')
  103.         f.seek(pos)
  104.         dat = f.read(siz)
  105.         f.close()
  106.         return dat
  107.  
  108.     
  109.     def _addval(self, val):
  110.         f = _open(self._datfile, 'rb+')
  111.         f.seek(0, 2)
  112.         pos = int(f.tell())
  113.         npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
  114.         f.write('\x00' * (npos - pos))
  115.         pos = npos
  116.         f.write(val)
  117.         f.close()
  118.         return (pos, len(val))
  119.  
  120.     
  121.     def _setval(self, pos, val):
  122.         f = _open(self._datfile, 'rb+')
  123.         f.seek(pos)
  124.         f.write(val)
  125.         f.close()
  126.         return (pos, len(val))
  127.  
  128.     
  129.     def _addkey(self, key, pos_and_siz_pair):
  130.         self._index[key] = pos_and_siz_pair
  131.         f = _open(self._dirfile, 'a')
  132.         self._chmod(self._dirfile)
  133.         f.write('%r, %r\n' % (key, pos_and_siz_pair))
  134.         f.close()
  135.  
  136.     
  137.     def __setitem__(self, key, val):
  138.         if type('') == type(''):
  139.             pass
  140.         elif not type('') == type(val):
  141.             raise TypeError, 'keys and values must be strings'
  142.         
  143.         if key not in self._index:
  144.             self._addkey(key, self._addval(val))
  145.         else:
  146.             (pos, siz) = self._index[key]
  147.             oldblocks = (siz + _BLOCKSIZE - 1) // _BLOCKSIZE
  148.             newblocks = (len(val) + _BLOCKSIZE - 1) // _BLOCKSIZE
  149.             if newblocks <= oldblocks:
  150.                 self._index[key] = self._setval(pos, val)
  151.             else:
  152.                 self._index[key] = self._addval(val)
  153.  
  154.     
  155.     def __delitem__(self, key):
  156.         del self._index[key]
  157.         self._commit()
  158.  
  159.     
  160.     def keys(self):
  161.         return self._index.keys()
  162.  
  163.     
  164.     def has_key(self, key):
  165.         return key in self._index
  166.  
  167.     
  168.     def __contains__(self, key):
  169.         return key in self._index
  170.  
  171.     
  172.     def iterkeys(self):
  173.         return self._index.iterkeys()
  174.  
  175.     __iter__ = iterkeys
  176.     
  177.     def __len__(self):
  178.         return len(self._index)
  179.  
  180.     
  181.     def close(self):
  182.         self._commit()
  183.         self._index = None
  184.         self._datfile = None
  185.         self._dirfile = None
  186.         self._bakfile = None
  187.  
  188.     __del__ = close
  189.     
  190.     def _chmod(self, file):
  191.         if hasattr(self._os, 'chmod'):
  192.             self._os.chmod(file, self._mode)
  193.         
  194.  
  195.  
  196.  
  197. def open(file, flag = None, mode = 438):
  198.     '''Open the database file, filename, and return corresponding object.
  199.  
  200.     The flag argument, used to control how the database is opened in the
  201.     other DBM implementations, is ignored in the dumbdbm module; the
  202.     database is always opened for update, and will be created if it does
  203.     not exist.
  204.  
  205.     The optional mode argument is the UNIX mode of the file, used only when
  206.     the database has to be created.  It defaults to octal code 0666 (and
  207.     will be modified by the prevailing umask).
  208.  
  209.     '''
  210.     
  211.     try:
  212.         um = _os.umask(0)
  213.         _os.umask(um)
  214.     except AttributeError:
  215.         pass
  216.  
  217.     mode = mode & ~um
  218.     return _Database(file, mode)
  219.  
  220.